package com.lemon.smartsafetynew.ui.activity.ledger;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolCheckItemSetVo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolCheckItemVo;
import com.lemon.smartsafetynew.net.services.SafePatrolServices;
import com.lemon.smartsafetynew.ui.adapter.ledger.LedgerCheckItemListAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Andrew on 2017/10/31.
 */

public class LedgerCheckItemListActivity extends SwipeBackActivity {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    private LedgerCheckItemListAdapter mAdapter;
    private List<SafePatrolCheckItemVo> mDataList = new ArrayList<>();

    private static String item_id;

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_SAFE_PATROL_ITEM_ID)) {
                item_id = bundle.getString(KeyConstants.KEY_SAFE_PATROL_ITEM_ID);
            }
        }
    }

    @Override
    protected void initTitle() {
        this.setBarTitle("检查标准");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(LedgerCheckItemListActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_ledger_check_item_list;
    }

    @Override
    protected void initViews() {
        super.initViews();

        initCommon();
        getCheckItemList();
    }

    /**
     * 初始化页面
     */
    private void initCommon() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(IApplication.context(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        mAdapter = new LedgerCheckItemListAdapter(mDataList);
        recyclerView.setAdapter(mAdapter);
    }

    /**
     * 获取检查项子项列表
     */
    private void getCheckItemList() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("item_id", item_id);
        SafePatrolServices
                .init()
                .getSafePatrolCheckItemList(params);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent event) {

        if (KeyConstants.URL_GET_SAFE_PATROL_CHECK_ITEM_LIST.equals(event.getTag())) {  //获取检查子项列表
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.show();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow() && recyclerView != null) {
                mHUDUtil.hidden();

                if (Constants.Result.SUCCESS == event.getEventResult()) {

                    if (event.getObj() instanceof SafePatrolCheckItemSetVo) {

                        SafePatrolCheckItemSetVo safePatrolCheckItemSetVo = (SafePatrolCheckItemSetVo) event.getObj();

                        if (mAdapter != null) {
                            mDataList.clear();
                            mAdapter.notifyDataSetChanged();
                            mAdapter.addItems(safePatrolCheckItemSetVo.getRows());
                        }
                    }


                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    //TODO
                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {
                            callFinish();
                        }
                    });
                }
            }
        }

    }

}
